using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;

namespace ORS.OrderRobot.EFCore
{
    public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<OrsDbContext>
    {
        public OrsDbContext CreateDbContext(string[] args)
        {
            // 获取解决方案根目录
            var solutionDir = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), ".."));
            
            IConfigurationRoot configuration = new ConfigurationBuilder()
                .SetBasePath(solutionDir)
                .AddJsonFile("WebApp/appsettings.json")
                .Build();

            var builder = new DbContextOptionsBuilder<OrsDbContext>();
            var connectionString = configuration.GetConnectionString(Consts.ConnectName);

            builder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));

            return new OrsDbContext(builder.Options);
        }
    }
} 